package com.fone.player.activity.search.adapter;

import java.util.List;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.activity.search.SearchResultActivity;
import com.fone.player.client.SearchRst.Cnt;
import com.fone.player.play.FonePlayerActivity;
import com.fone.player.play.PlayerAction;
import com.fone.player.play.PlayerFrom;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;

public class SearchResultAdapter extends BaseAdapter {

	private List<Cnt> mList;
	private String TAG = "SearchResultAdapter";

	public SearchResultAdapter(List<Cnt> list) {
		this.mList = list;
	}

	@Override
	public int getCount() {
		if (mList != null) {
			int size = mList.size();
			return size % 3 == 0 ? size / 3 : size / 3 + 1;
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {

		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(ApplicationManage.getGlobalContext()).inflate(
					R.layout.search_result_gridview, null);
			viewHolder = new ViewHolder();
			viewHolder.gridView = (GridView) convertView
					.findViewById(R.id.gridview);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		int end = 0;
		int lastPosition = mList.size();
		if (((position + 1) * 3) > lastPosition) {
			end = lastPosition;
		} else {
			end = (position + 1) * 3;
		}
		List<Cnt> list = mList.subList(position * 3, end);
		
		viewHolder.gridView.setAdapter(new GridViewAdapter(ApplicationManage.getGlobalContext(), list));
		viewHolder.gridView.setOnItemClickListener(new MyitemClickListener(list));
		
//		GridViewAdapter adapter = null;
//		ListAdapter listAdapter = viewHolder.gridView.getAdapter();
//		if (listAdapter != null && listAdapter instanceof GridViewAdapter) {
//			adapter = (GridViewAdapter) listAdapter;
//			adapter.setData(list);
//			adapter.notifyDataSetChanged();
//		} else {
//			adapter = new GridViewAdapter(ApplicationManage.getGlobalContext(), list);
//			viewHolder.gridView.setAdapter(adapter);
//		}
		
//		viewHolder.gridView.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view,
//					int position, long id) {
//				if (FoneUtil.isNetOkWithToast(ApplicationManage.getGlobalContext())) {
//					Cnt cnt = list.get(position);
//					String url = list.get(position).url;
//					L.i(TAG, "video url:" + url);
//					Intent intent = new Intent(ApplicationManage.getGlobalContext(),
//							FonePlayerActivity.class);
//					L.i(TAG, "SearchResultActivity.mSearchType"
//							+ SearchResultActivity.mSearchType);
////					if (SearchResultActivity.mSearchType == SearchActivity.SEARCH_HOT_WORD) {
////						intent.putExtra("from", PlayerFrom.SEARCH_ENTRANCE);
////					} else {
//						intent.putExtra("from", PlayerFrom.SEARCH_NORMAL);
////					}
//					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					intent.putExtra("name", cnt.name);
//					if (1 == cnt.utp) {
//						intent.putExtra("action", PlayerAction.DETAIL.name());
//						intent.putExtra("vgdetail", url);
//					} else {
//						intent.putExtra("action", PlayerAction.FULLPLAY.name());
//						intent.putExtra("xyzplay", url);
//					}
//					if(SearchResultActivity.searchResultActivity!=null){
//						SearchResultActivity.searchResultActivity.startActivity(intent);
//					}
//				}
//			}
//		});
		return convertView;
	}

	static class ViewHolder {
		public GridView gridView;
	}

	public void removeData(){
		mList = null;
	}
	
	private class MyitemClickListener implements OnItemClickListener{

		List<Cnt> list;
		public MyitemClickListener(List<Cnt> list){
			this.list = list;
		}
		
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if (FoneUtil.isNetOkWithToast(ApplicationManage.getGlobalContext())) {
				Cnt cnt = list.get(position);
				String url = list.get(position).url;
				L.i(TAG, "video url:" + url);
				Intent intent = new Intent(ApplicationManage.getGlobalContext(),
						FonePlayerActivity.class);
				L.i(TAG, "SearchResultActivity.mSearchType"
						+ SearchResultActivity.mSearchType);

				intent.putExtra("from", PlayerFrom.SEARCH_NORMAL);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("name", cnt.name);
				if (1 == cnt.utp) {
					intent.putExtra("action", PlayerAction.DETAIL.name());
					intent.putExtra("vgdetail", url);
				} else {
					intent.putExtra("action", PlayerAction.FULLPLAY.name());
					intent.putExtra("xyzplay", url);
				}
				if(SearchResultActivity.searchResultActivity!=null){
					SearchResultActivity.searchResultActivity.startActivity(intent);
				}
			}
			
		}
		
	}
	
}
